package com.hnas.eCard.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.persistence.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.hnas.account.orm.Account;
import com.hnas.common.util.CommonUtils;
import com.hnas.core.dao.BaseDaoImpl;
import com.hnas.core.dao.PageBean;
import com.hnas.core.dto.DTOPagedList;
import com.hnas.core.util.DateUtil;
import com.hnas.core.util.StringUtil;
import com.hnas.eCard.dao.CardTrailDAO;
import com.hnas.eCard.domain.CardTrailStatus;
import com.hnas.eCard.domain.ECardCommonFinalValue;
import com.hnas.eCard.domain.ECardStatus;
import com.hnas.eCard.domain.SearchECardStatus;
import com.hnas.eCard.orm.CardTrail;
import com.hnas.eCard.service.dto.DTOSaleCardOrderDetail;

@Component("cardTrailDAO")
public class CardTrailDAOImpl extends BaseDaoImpl<CardTrail> implements CardTrailDAO {
	
	private static Logger logger = LoggerFactory.getLogger(CardTrailDAOImpl.class);

	@Value("${send_card_time_limit}")
	private int sendCardTimeLimit;
	
	public CardTrailDAOImpl() {
		super(CardTrail.class);
	}

	@Override
	public List<CardTrail> findCardTrailByStatus(String accId, String phoneNo, String searchStatus, Integer pageNo, Integer pageSize) throws Exception {
		DTOPagedList<CardTrail> pageList = new DTOPagedList<CardTrail>();
		PageBean<CardTrail> page = new PageBean<CardTrail>();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		StringBuffer sb = new StringBuffer();
		List<Object> paras = new ArrayList<Object>();
		int j = 1;
		sb.append(" from CardTrail t where t.isDeleted = 0 ");
		if (StringUtil.isNotEmpty(searchStatus)) {
			String tempStatus = null;
			if (searchStatus.trim().equals(SearchECardStatus.RECEIVED)) {// 已收到:已领取，待领取
				if (StringUtil.isNotEmpty(accId) && StringUtil.isNotEmpty(phoneNo)) {
					tempStatus = "'" + CardTrailStatus.CONFIRM_RECEIVE_GIVE + "','" + CardTrailStatus.REFUSE + "','"+CardTrailStatus.CANCEL_GIVE+"','"+CardTrailStatus.OVERTIME+"'";
					sb.append(" and ((t.receiveAccId =  ?"+ j++ +" and t.status in ("+ tempStatus + " ))");
					paras.add(accId.trim());
					sb.append(" or (t.receivePhoneNo =  ?"+ j++ +" and (t.status in ('" +CardTrailStatus.CANCEL_GIVE+"','"+CardTrailStatus.OVERTIME+"') or (t.status = '"+CardTrailStatus.RECEIVING_GIVING+"' and timestampdiff(second,t.createTime,now()) < ?"+j++ +"))))");
					paras.add(phoneNo.trim());
					paras.add(sendCardTimeLimit);
					sb.append(" order by t.sort desc, t.updateTime desc ");
				}
			} else if (searchStatus.trim().equals(SearchECardStatus.PRESENTED)) {// 已赠送:赠送中，已赠送，赠送被拒绝
				if (StringUtil.isNotEmpty(accId)) {
					sb.append(" and t.sendAccId =  ?" + j++);
					paras.add(accId.trim());
					tempStatus = "'" + CardTrailStatus.RECEIVING_GIVING + "','" + CardTrailStatus.CONFIRM_RECEIVE_GIVE + "','" + CardTrailStatus.REFUSE + "','"+CardTrailStatus.CANCEL_GIVE+"','"+CardTrailStatus.OVERTIME+"'";
					sb.append(" and t.status in ("+ tempStatus + " )");
					//针对已赠送的只查询最近30天的数据
					sb.append(" and t.createTime >= '" + DateUtil.dateTimeToString(DateUtil.addDate(new Date(), -30)) + "'");
					sb.append(" order by t.sort desc, t.updateTime desc ");
				}
			}
		}
		page.setQuery(sb.toString());
		page.setQueryCount("select count(t.id) " + sb);
		this.queryByPage(page, paras.toArray());
		pageList.setPageNo(page.getPageNo());
		pageList.setPageSize(page.getPageSize());
		pageList.setTotalCount(page.getTotalCount());
		//在这里过滤
		
		return page.getList();
	}
	
	//@Override
	public List<CardTrail> findCardTrailByStatus_new(String accId, String phoneNo, String searchStatus, Integer pageNo, Integer pageSize) throws Exception {
		if (StringUtil.isNotEmpty(searchStatus)) {
			if (searchStatus.trim().equals(SearchECardStatus.RECEIVED)) {// 已收到:已领取，待领取
				if (StringUtil.isNotEmpty(accId) && StringUtil.isNotEmpty(phoneNo)) {
					return queryReceive(pageNo, pageSize, accId, phoneNo);
				}
			} else if (searchStatus.trim().equals(SearchECardStatus.PRESENTED)) {// 已赠送:赠送中，已赠送，赠送被拒绝
				if (StringUtil.isNotEmpty(accId)) {
					return querySend(pageNo, pageSize, accId);
				}
			}
		}
		return null;
	}

	/**
	 * 查询已赠送
	 * @Title: querySend  
	 * @Description:  
	 * @param pageNo
	 * @param pageSize
	 * @param sendAccId
	 * @return
	 * @throws
	 */
	private List<CardTrail> querySend(int pageNo,int pageSize,String sendAccId) {
		String tempStatus = "'" + CardTrailStatus.RECEIVING_GIVING + "','" + CardTrailStatus.CONFIRM_RECEIVE_GIVE + "','" 
						 + CardTrailStatus.REFUSE + "','"+CardTrailStatus.CANCEL_GIVE+"','"+CardTrailStatus.OVERTIME+"'";
		StringBuffer sb = new StringBuffer();
		sb.append("select * from tbl_card_trail where send_acc_id = ?1 and status in ("+tempStatus+")");
		sb.append(" and create_time >= '" + DateUtil.dateTimeToString(DateUtil.addDate(new Date(), -30)) + "'");
		sb.append(" order by sort desc, update_time desc ");
		Query query = this.entityManager.createNativeQuery(sb.toString(),CardTrail.class);
		query.setFirstResult((pageNo-1)*pageSize);
		query.setMaxResults(pageSize);
		query.setParameter(1, sendAccId);
		return query.getResultList();
	}

	/**
	 * 查询已收到
	 * @Title: queryReceive  
	 * @Description:  
	 * @param pageNo
	 * @param pageSize
	 * @param receiveAccId
	 * @param phoneNo
	 * @return
	 * @throws
	 */
	private List<CardTrail> queryReceive(int pageNo,int pageSize,String receiveAccId,String phoneNo) {
		String tempStatus = "'" + CardTrailStatus.CONFIRM_RECEIVE_GIVE + "','" + CardTrailStatus.REFUSE + "','"+CardTrailStatus.CANCEL_GIVE+"','"+CardTrailStatus.OVERTIME+"'";
		StringBuffer sb = new StringBuffer();
		sb.append("select * from tbl_card_trail where is_deleted = 0 and status in ("+tempStatus+")");
		sb.append(" and ((receive_acc_id =  ?1 and status in ("+ tempStatus + " ))");
		sb.append(" or (receive_phone_no =  ?2 and (status in ('" +CardTrailStatus.CANCEL_GIVE+"','"+CardTrailStatus.OVERTIME+"') or (status = '"+CardTrailStatus.RECEIVING_GIVING+"' and timestampdiff(second,create_time,now()) < ?3))))");
		sb.append(" order by sort desc, update_time desc ");
		Query query = this.entityManager.createNativeQuery(sb.toString(),CardTrail.class);
		query.setFirstResult((pageNo-1)*pageSize);
		query.setMaxResults(pageSize);
		query.setParameter(1, receiveAccId);
		query.setParameter(2, phoneNo);
		query.setParameter(3, sendCardTimeLimit);
		return query.getResultList();
	}
	
	private List<CardTrail> queryAll(int pageNo,int pageSize) {
		String tempStatus = "'" + CardTrailStatus.CONFIRM_RECEIVE_GIVE + "','" + CardTrailStatus.REFUSE + "','"+CardTrailStatus.CANCEL_GIVE+"','"+CardTrailStatus.OVERTIME+"'";
		StringBuffer sb = new StringBuffer();
		sb.append("select * from tbl_card_trail where is_deleted = 0 ");
		sb.append(" order by sort desc, updateTime desc ");
		Query query = this.entityManager.createNativeQuery(sb.toString(),CardTrail.class);
		query.setFirstResult((pageNo-1)*pageSize);
		query.setMaxResults(pageSize);
		return query.getResultList();
	}

	@Override
	public CardTrail saveOrUpdate(CardTrail dto) throws Exception {
		CardTrail vo = null;
		if (null != dto) {
			if (StringUtil.isNotEmpty(dto.getId())) {
				vo = this.find(dto.getId());
			}

			if (null == vo) {
				vo = dto;
				vo.setCreateTime(DateUtil.dateTimeToString(new Date()));
				vo.setUpdateTime(DateUtil.dateTimeToString(new Date()));
				vo.setIsDeleted(false);
				this.persist(vo);
			} else {
				BeanUtils.copyProperties(dto, vo, new String[] { "id", "createTime" });
				vo.setUpdateTime(DateUtil.dateTimeToString(new Date()));
				this.merge(vo);
			}
		}
		return vo;
	}

	@Override
	public List<CardTrail> findCardsByStatus(String receiveAccId, String receivePhoneNo, String status) {
		String hql = " from CardTrail t where  t.isDeleted = 0 ";
		int index = 1;
		Map<Integer, String> map = new HashMap<Integer, String>();
		if (StringUtil.isNotEmpty(receiveAccId)) {
			hql += " and t.receiveAccId = ?";
			map.put(index++, receiveAccId);
		}
		if (StringUtil.isNotEmpty(receivePhoneNo)) {
			hql += " and t.receivePhoneNo = ?";
			map.put(index++, receivePhoneNo);
		}
		if (StringUtil.isNotEmpty(status)) {
			hql += " and t.status = ?";
			map.put(index++, status);
		}
		Query query = this.entityManager.createQuery(hql, CardTrail.class);
		if (!CollectionUtils.isEmpty(map)) {
			for (Entry<Integer, String> entry : map.entrySet()) {
				query.setParameter(entry.getKey(), entry.getValue());
			}
		}
		return query.getResultList();
	}

	@Override
	public List<CardTrail> searchCardTrailSql(CardTrail vo) {
		StringBuffer sb = new StringBuffer();
		List<Object> paras = new ArrayList<Object>();
		if (null != vo) {
			int j = 1;
			if (StringUtil.isNotEmpty(vo.getId())) {
				sb.append(" and t.id =?" + j++);
				paras.add(vo.getId().trim());
			}
			if (StringUtil.isNotEmpty(vo.getSendAccId())) {
				sb.append(" and t.sendAccId =?" + j++);
				paras.add(vo.getSendAccId().trim());
			}
			if (StringUtil.isNotEmpty(vo.getSendPhoneNo())) {
				sb.append(" and t.sendPhoneNo =?" + j++);
				paras.add(vo.getSendPhoneNo().trim());
			}
			if (StringUtil.isNotEmpty(vo.getSendName())) {
				sb.append(" and t.sendName =?" + j++);
				paras.add(vo.getSendName().trim());
			}
			if (StringUtil.isNotEmpty(vo.getSendCardNo())) {
				sb.append(" and t.sendCardNo =?" + j++);
				paras.add(vo.getSendCardNo().trim());
			}
			if (StringUtil.isNotEmpty(vo.getReceiveAccId())) {
				sb.append(" and t.receiveAccId =?" + j++);
				paras.add(vo.getReceiveAccId().trim());
			}

			if (StringUtil.isNotEmpty(vo.getReceivePhoneNo())) {
				sb.append(" and t.receivePhoneNo =?" + j++);
				paras.add(vo.getReceivePhoneNo().trim());
			}
			if (StringUtil.isNotEmpty(vo.getReceiveName())) {
				sb.append(" and t.receiveName =?" + j++);
				paras.add(vo.getReceiveName().trim());
			}
			if (StringUtil.isNotEmpty(vo.getStatus())) {
				sb.append(" and t.status =?" + j++);
				paras.add(vo.getStatus().trim());
			}
			if (null != vo.getIsDeleted()) {
				sb.append(" and t.isDeleted =?" + j++);
				paras.add(vo.getIsDeleted());
			}
			if (StringUtil.isNotEmpty(vo.getShareCode())) {
				sb.append(" and t.shareCode =?" + j++);
				paras.add(vo.getShareCode().trim());
			}
		}
		if (sb.length() > 0) {
			sb.delete(0, 5);
			sb.insert(0, " from CardTrail t where ");
		} else {
			sb.append(" from CardTrail t");
		}
		sb.append(" order by t.createTime DESC");
		List<CardTrail> list = this.findByQuery(sb.toString(), paras.toArray());
		return list;
	}

	@Override
	public List<CardTrail> findCardTrailsByCardNoAndSomeStatus(String cardNo, String someStatus, String isDeleted) {
		StringBuffer sb = new StringBuffer();
		List<Object> paras = new ArrayList<Object>();
			int j = 1;
			if (StringUtil.isNotEmpty(cardNo)) {
				sb.append(" and t.sendCardNo =?" + j++);
				paras.add(cardNo.trim());
			}
			if (StringUtil.isNotEmpty(someStatus)) {
				sb.append(" and t.status in (" + StringUtil.getChangeString(someStatus.trim())+ ")");
			}
			if (StringUtil.isNotEmpty(isDeleted)) { // isDeleted  传 0 [不删除] 或 1 [删除]
				sb.append(" and t.isDeleted =?" + j++);
				paras.add(isDeleted);
			}
		if (sb.length() > 0) {
			sb.delete(0, 5);
			sb.insert(0, " from CardTrail t where ");
		} else {
			sb.append(" from CardTrail t");
		}
		sb.append(" order by t.createTime DESC");
		List<CardTrail> list = this.findByQuery(sb.toString(), paras.toArray());
		return list;
	}

	@Override
	public void cancelGiveUpdate(String id, String id2) {
		String sql = "update tbl_acc_e_card set status = ?1,update_time=?2 where id = ?3 and status = ?4 and is_deleted=?5";
		String sql2 = "update tbl_card_trail set status = ?1,update_time=?2,sort=?3 where id = ?4 and status = ?5 and is_deleted=?6";
		Query query = this.entityManager.createNativeQuery(sql);
		query.setParameter(1, ECardStatus.NORMAL);
		query.setParameter(2, DateUtil.dateTimeToString(new Date()));
		query.setParameter(3, id);
		query.setParameter(4, ECardStatus.GIVING);
		query.setParameter(5, false);
		Query query2 = this.entityManager.createNativeQuery(sql2);
		query2.setParameter(1, CardTrailStatus.CANCEL_GIVE);
		query2.setParameter(2, DateUtil.dateTimeToString(new Date()));
		query2.setParameter(3, ECardCommonFinalValue.SORT_TAIL);
		query2.setParameter(4, id2);
		query2.setParameter(5, CardTrailStatus.RECEIVING_GIVING);
		query2.setParameter(6, false);
		
		query.executeUpdate();
		query2.executeUpdate();
	}

	@Override
	public void receiveECardUpdate(String id, String id2,String receiveAccId) {
		String accECardSql = "update tbl_acc_e_card set status = ?1,update_time=?2,acc_id=?3 where id = ?4 and status = ?5 and is_deleted=?6";
		String ecardTrailSql = "update tbl_card_trail set status = ?1,update_time=?2,receive_acc_id=?3,sort=?4 where id = ?5 and status = ?6 and is_deleted=?7";
		Query accECardQuery = this.entityManager.createNativeQuery(accECardSql);
		accECardQuery.setParameter(1, ECardStatus.NORMAL);
		accECardQuery.setParameter(2, DateUtil.dateTimeToString(new Date()));
		accECardQuery.setParameter(3, receiveAccId);
		accECardQuery.setParameter(4, id);
		accECardQuery.setParameter(5, ECardStatus.GIVING);
		accECardQuery.setParameter(6, false);
		Query ecardTrailQuery = this.entityManager.createNativeQuery(ecardTrailSql);
		ecardTrailQuery.setParameter(1, CardTrailStatus.CONFIRM_RECEIVE_GIVE);
		ecardTrailQuery.setParameter(2, DateUtil.dateTimeToString(new Date()));
		ecardTrailQuery.setParameter(3, receiveAccId);
		ecardTrailQuery.setParameter(4, ECardCommonFinalValue.SORT_TAIL);
		ecardTrailQuery.setParameter(5, id2);
		ecardTrailQuery.setParameter(6, CardTrailStatus.RECEIVING_GIVING);
		ecardTrailQuery.setParameter(7, false);
		
		accECardQuery.executeUpdate();
		ecardTrailQuery.executeUpdate();
	}

	@Override
	public void refuseECardUpdate(String id, String id2,String receiveAccId) {
		String accECardSql = "update tbl_acc_e_card set status = ?1,update_time=?2 where id = ?3 and status = ?4 and is_deleted=?5";
		String ecardTrailSql = "update tbl_card_trail set status = ?1,update_time=?2,receive_acc_id=?3,sort=?4 where id = ?5 and status = ?6 and is_deleted=?7";
		Query accECardQuery = this.entityManager.createNativeQuery(accECardSql);
		accECardQuery.setParameter(1, ECardStatus.NORMAL);
		accECardQuery.setParameter(2, DateUtil.dateTimeToString(new Date()));
		accECardQuery.setParameter(3, id);
		accECardQuery.setParameter(4, ECardStatus.GIVING);
		accECardQuery.setParameter(5, false);
		Query ecardTrailQuery = this.entityManager.createNativeQuery(ecardTrailSql);
		ecardTrailQuery.setParameter(1, CardTrailStatus.REFUSE);
		ecardTrailQuery.setParameter(2, DateUtil.dateTimeToString(new Date()));
		ecardTrailQuery.setParameter(3, receiveAccId);
		ecardTrailQuery.setParameter(4, ECardCommonFinalValue.SORT_TAIL);
		ecardTrailQuery.setParameter(5, id2);
		ecardTrailQuery.setParameter(6, CardTrailStatus.RECEIVING_GIVING);
		ecardTrailQuery.setParameter(7, false);
		accECardQuery.executeUpdate();
		ecardTrailQuery.executeUpdate();
	}

	@Override
	public List<CardTrail> listByCardAndDate(String cardNo, String date) {
		String sql = "select * from tbl_card_trail where send_card_no = ?1 and create_time > ?2 order by create_time asc";
		Query query = this.entityManager.createNativeQuery(sql, CardTrail.class);
		query.setParameter(1, cardNo);
		query.setParameter(2, date);
		return query.getResultList();
	}

	@Override
	public List<CardTrail> listByCards(String sendAccId, String sendPhoneNo, String receivePhoneNo,
			String changeString) {
		String sql = "select * from tbl_card_trail where send_acc_id = ?1 and send_phone_no = ?2 and receive_phone_no = ?3 and send_card_no in ("+changeString+")";
		Query query = this.entityManager.createNativeQuery(sql, CardTrail.class);
		query.setParameter(1, sendAccId);
		query.setParameter(2, sendPhoneNo);
		query.setParameter(3, receivePhoneNo);
		return query.getResultList();
	}

	@Override
	public int batchSave(String sendAccId, DTOSaleCardOrderDetail orderDetailDto) {
		StringBuffer sb = new StringBuffer();
		String[] cardNos = orderDetailDto.getCards().split(",");
		sb.append("insert into tbl_card_trail(id,send_acc_id,send_phone_no,send_name,send_card_no,send_msg,receive_phone_no,"
				+ "receive_name,status,current_balance,remark,open_id,sort,create_type,create_time,update_time,is_deleted) values ");
		if(cardNos == null || cardNos.length <= 0) return 0;
		for(int i = 0; i < cardNos.length; i++) {
			sb.append("('").append(CommonUtils.getUUID()).append("','")
			.append(sendAccId).append("','")
			.append(orderDetailDto.getSendPhoneNo()).append("','")
			.append(orderDetailDto.getSendName()).append("','")
			.append(cardNos[i]).append("','")
			.append(orderDetailDto.getSendMsg()).append("','")
			.append(orderDetailDto.getReceivePhoneNo()).append("','")
			.append(orderDetailDto.getReceiveName()).append("','")
			.append(CardTrailStatus.RECEIVING_GIVING).append("','")
			.append(orderDetailDto.getDenomination()).append("','")
			.append("购卡给他人").append("','")
			.append(orderDetailDto.getOpenid()).append("','")
			.append(ECardCommonFinalValue.SORT_HEAD).append("',")
			.append(1).append(",'")
			.append(DateUtil.dateTimeToString(new Date())).append("','")
			.append(DateUtil.dateTimeToString(new Date())).append("',")
			.append("0").append("),");
		}
		String sql = sb.substring(0, sb.length() -1);
		logger.info("批量插入sql："+sql);
		Query query = this.entityManager.createNativeQuery(sql);
		return query.executeUpdate();
	}

	@Override
	public void receiveSaleForOtherECard(String accId, CardTrail cardTrail) {
//		String accECardSql = "insert into tbl_acc_e_card(create_time, creator, is_deleted, update_time, updater, acc_id, acc_no, card_mark, card_no, expiry_date, face_value, remark, status, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		String ecardTrailSql = "update tbl_card_trail set status = ?1,update_time=?2,receive_acc_id=?3,sort=?4 where id = ?5 and status = ?6 and is_deleted=?7";
//		Query accECardQuery = this.entityManager.createNativeQuery(accECardSql);
//		accECardQuery.setParameter(1, DateUtil.dateTimeToString(new Date()));
//		accECardQuery.setParameter(2, vo.getAccId());
//		accECardQuery.setParameter(3, false);
//		accECardQuery.setParameter(4, DateUtil.dateTimeToString(new Date()));
//		accECardQuery.setParameter(5, vo.getAccId());
//		accECardQuery.setParameter(6, vo.getAccId());
//		accECardQuery.setParameter(7, null);
//		accECardQuery.setParameter(8, CardMarkConstant.ELEC_CARD);
//		accECardQuery.setParameter(9, vo.getCardNo());
//		accECardQuery.setParameter(10, null);
//		accECardQuery.setParameter(11, vo.getFaceValue());
//		accECardQuery.setParameter(12, vo.getRemark());
//		accECardQuery.setParameter(13, vo.getStatus());
//		accECardQuery.setParameter(14, vo.getId());
		Query ecardTrailQuery = this.entityManager.createNativeQuery(ecardTrailSql);
		ecardTrailQuery.setParameter(1, CardTrailStatus.CONFIRM_RECEIVE_GIVE);
		ecardTrailQuery.setParameter(2, DateUtil.dateTimeToString(new Date()));
		ecardTrailQuery.setParameter(3, accId);
		ecardTrailQuery.setParameter(4, ECardCommonFinalValue.SORT_TAIL);
		ecardTrailQuery.setParameter(5, cardTrail.getId());
		ecardTrailQuery.setParameter(6, CardTrailStatus.RECEIVING_GIVING);
		ecardTrailQuery.setParameter(7, false);
		ecardTrailQuery.executeUpdate();
//		accECardQuery.executeUpdate();
		
	}

	@Override
	public void shareReceiveECard(Account account, CardTrail cardTrail) {
		String ecardTrailSql = "update tbl_card_trail set status = ?1,update_time=?2,receive_acc_id=?3,sort=?4,receive_wx_image=?5,receive_wx_name=?6,receive_phone_no =?7 where id = ?8 and status = ?9 and is_deleted=?10";
		Query ecardTrailQuery = this.entityManager.createNativeQuery(ecardTrailSql);
		ecardTrailQuery.setParameter(1, CardTrailStatus.CONFIRM_RECEIVE_GIVE);
		ecardTrailQuery.setParameter(2, DateUtil.dateTimeToString(new Date()));
		ecardTrailQuery.setParameter(3, account.getId());
		ecardTrailQuery.setParameter(4, ECardCommonFinalValue.SORT_TAIL);
		ecardTrailQuery.setParameter(5, account.getHeadPhoto());
		ecardTrailQuery.setParameter(6, account.getNickName());
		ecardTrailQuery.setParameter(7, account.getPhoneNo());
		ecardTrailQuery.setParameter(8, cardTrail.getId());
		ecardTrailQuery.setParameter(9, CardTrailStatus.RECEIVING_GIVING);
		ecardTrailQuery.setParameter(10, false);
		ecardTrailQuery.executeUpdate();
	}

	@Override
	public List<CardTrail> findCardTrailsByCardNo(String cardNo) {
		String sql = "select * from tbl_card_trail where send_card_no = ?1 order by create_time asc";
		Query query = this.entityManager.createNativeQuery(sql,CardTrail.class);
		query.setParameter(1, cardNo);
		return query.getResultList();
	}

	@Override
	public CardTrail findByShareCode(String shareCode) {
		String sql = "select * from tbl_card_trail where share_code = ?1 order by create_time asc";
		Query query = this.entityManager.createNativeQuery(sql,CardTrail.class);
		query.setParameter(1, shareCode);
		List<CardTrail> resultList = query.getResultList();
		if(resultList != null && resultList.size() > 0) {
			return resultList.get(0);
		}
		return null;
	}


}
